package com.computaris.script;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.computaris.ws.Event;
import com.computaris.ws.EventImpl;

public class BillSettlementScript {
	public static void main(String[] args) {
		try {
			System.out.println(args.length);
			for (int i = 0; i < args.length; i++) {
				System.out.println(args[i]);
			}
			System.out.println("-- end args --");
			Date begin = null;
			if(args.length <= 1 || (args.length == 2 && !"-s".equals(args[0])) || (args.length == 4 && !"-s".equals(args[2]))) {
				Calendar cal = Calendar.getInstance();
				cal.set(Calendar.DAY_OF_MONTH, 2);
				cal.set(Calendar.HOUR_OF_DAY, 0);
				cal.set(Calendar.MINUTE, 0);
				cal.set(Calendar.SECOND, 0);
				begin = cal.getTime();
			} else {
				DateFormat df = new SimpleDateFormat("yyyyMMdd");
				if("-s".equals(args[0])) {
					begin = df.parse(args[1]);
				} else {
					begin = df.parse(args[3]);
				}
				Calendar cal = Calendar.getInstance();
				cal.setTime(begin);
				cal.add(Calendar.DAY_OF_YEAR, -1);
				begin = cal.getTime();
			}
			
			String msisdn = null;
			if(args.length > 1 && "-m".equals(args[0])) {
				msisdn = args[1];
			} else if(args.length > 2 && "-m".equals(args[0])) {
				msisdn = args[3];
			}

			EventImpl impl = new EventImpl();
			List<Event> allEvents = impl.getAllEvents();
			
			Map<String, SubscriberData> map = new HashMap<String, SubscriberData>();
			
			DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
			String fileTime = formatter.format(new Date());
			Properties props = new Properties();
			System.out.println("loading properties...");
			props.load(new FileInputStream(System.getProperty("app.propertyFile")));
			PrintWriter pw = new PrintWriter(new FileWriter(props.getProperty("outputLocation") + 
					"report"
					//props.getProperty("outputFile")
					+ fileTime + ".report"));
			for(Event event : allEvents) {
				if(event.getEventDate().after(begin)) {
					if(msisdn != null && !msisdn.equals("" + event.getSubsriberId())) {
						continue;
					}
					SubscriberData data = map.get("" + event.getSubsriberId());
					if(data == null) {
						data = new SubscriberData("" + event.getSubsriberId());
						map.put(data.msisdn, data);
					}
					
					if(event.getType() == 0) {
						data.nrVoice++;
						data.costVoice += event.getCharge();
					} else if(event.getType() == 1) {
						data.nrSms++;
						data.costSms += event.getCharge();
					} else if(event.getType() == 2) {
						data.nrMms++;
						data.costMms += event.getCharge() + (event.getAmount() > 0 ? 100 : 0);
					} else if(event.getType() == 3) {
						data.nrData++;
						data.costData += event.getCharge();
					}
				}
			}
			
			formatter = new SimpleDateFormat("dd-MM-yyyy");
			String from = formatter.format(begin);
			String to = formatter.format(new Date());
			for(SubscriberData data : map.values()) {
				pw.println("Bill Period: " + from + " - " + to);
				pw.println("MSISDN - " + data.msisdn);
				pw.println("Sms : " + data.nrSms + " sms - " + data.costSms);
				pw.println("Mms : " + data.nrMms + " sms - " + data.costMms);
				pw.println("Voice : " + data.nrVoice + " calls - " + data.costVoice);
				pw.println("Data : " + data.nrData + " sessions - " + data.costData);
			}
			pw.close();
			
			System.out.println("job finished");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
